/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.stefanini.mundial2014.controller;

import com.fifa.filters.SecurityFilter;
import com.stefanini.mundial2014.ejbs.UserManagement;
import com.stefanini.persitencia.Usuario;
import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Lady
 */
@Named(value = "loginController")
@RequestScoped

public class LoginController implements Serializable{
    private static final long serialVersionUID =1L;
    
    private String usuario;
    private String password;
    private String grupo;

   
    @EJB
    private UserManagement userManagement;
    
    public void login(){
        Usuario user = userManagement.verificarLogin(usuario,password);
        FacesContext ctx = FacesContext.getCurrentInstance();
         
        if(user==null){
            ctx.addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error",
                            "Usuario o contraseña invalido"));
        } else {
            HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
            session.setAttribute(SecurityFilter.LOGGED_USER, user.getUsername());
            session.setAttribute(SecurityFilter.IS_LOGGED_IN,Boolean.TRUE);
            try {
                ctx.getExternalContext().redirect("/Mundial2014/pages/index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }        
   public void ChangeLanguage(){
   if (FacesContext.getCurrentInstance()
           .getViewRoot().getLocale().equals(new Locale("es"))){
       FacesContext.getCurrentInstance()
           .getViewRoot().setLocale(Locale.ENGLISH);
   }
   else{
       FacesContext.getCurrentInstance()
               .getViewRoot().setLocale(new Locale("es"));
   }
  }
    public void logout(){
    FacesContext ctx =FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
    session.invalidate();
    try{
        ctx.getExternalContext().redirect("/Mundial2014/login.xhtml");
    }catch(IOException ex){
        Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, "Error al redireccionar", ex);
    }
    }
    
    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public String getGrupo() {
        return grupo;
    }

    public void setGrupo(String grupo) {
        this.grupo = grupo;
    }
            
}
